﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.CRM.Bussiness;
using System.Data;
using ZX.BLL.Extend;
using ZX.EHR.Library;
using ZX.DBModule;
using ZX.BLL;
using System.Web.Security;
using ZX.CRM.Library;
namespace ZX.WebApp.Crm
{
    public partial class ChangeAccount : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void btnDone_Click(object sender, EventArgs e)
        {
            string loginName ="";//操作人登录名
            string uName= txtPM.Value;//被切换人名字
            try
            {
                loginName = this.User.LoginName;
            }
            catch (Exception)
            {
                loginName = "";
                throw;
            }
            FormsAuthentication.SignOut();
            Session["User"] = null;
            Session["IsOutSite"] = null;
            UserLogin(loginName, uName);
        }
        protected void btnAuth_Click(object sender, EventArgs e)
        {
            BaseBussnies buss = new BaseBussnies();
            CrmAuth ca = new CrmAuth();
            ca.AuthID = OldCus.Value == "" ? Guid.Empty : new Guid(OldCus.Value);
            ca.OwnID = NewCus.Value == "" ? Guid.Empty : new Guid(NewCus.Value);
            buss.entity = ca;
            buss.Insert();
        }
        /// <summary>
        /// 写登录日志
        /// </summary>
        private void WriteLog(DatUser du, string loginName)
        {
            BaseBussnies buss = new BaseBussnies();

            SysDataLog log = new SysDataLog();
            log.UserID = du.UserID;
            log.Action = "Login";
            //用户切换日志
            log.Data = "操作者IP:" + this.Request.UserHostAddress + ",FromAccount:" + loginName;
            log.EntityName = "DatUser";
            log.LogTime = DateTime.Now;
            buss.entity = log;
            buss.Insert();
        }
        private void UserLogin(string loginName, string uName)
        {
            DatUser user = new DatUser();
            user.UName = uName;
            user.UserID = PM.Value==""?Guid.Empty: new Guid(PM.Value);
            DatUser dtuser = new DatUser();
            BUser buser = new BUser();
            dtuser=buser.GetDatUserByLoginName(user);
            WriteLog(user, loginName);
            FormsAuthentication.SetAuthCookie(dtuser.LoginName, true);
            Session["User"] = dtuser;
            Session["IsOutSite"] = dtuser.IsOutsite;
            //Response.Redirect("../index.aspx");
            Response.Write("<Script language='JavaScript'>window.parent.location.href = window.parent.location.href;</Script>");
        }
    }
}